package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.telecom.cms.dto.CampCamp;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CampCampDAO {
	Dao dao = new DaoImp();

	private static List instlist=null;
	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampCamp transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Camp_Camp where Camp_Id="
				+ transientInstance.getCampId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Camp set latn_id='"
						+ transientInstance.getLatnId() + "',camp_name='"
						+ transientInstance.getCampName()
						+ "',target_cust_desc='"
						+ transientInstance.getTargetCustDesc()
						+ "',cluster_type='"
						+ transientInstance.getClusterType()
						+ "',camp_target='" + transientInstance.getCampTarget()
						+ "',scheme_book='" + transientInstance.getSchemeBook()
						+ "',prd_inst_desc='"
						+ transientInstance.getPrdInstDesc()
						+ "',camp_script='" + transientInstance.getCampScript()
						+ "',camp_status='" + transientInstance.getCampStatus()
						+ "',start_date=date '"
						+ (transientInstance.getStartDate().indexOf('0')>-1?transientInstance.getStartDate():CurrentTime.getCurrentDate2())
						+ "',end_date=date '"
						+ (transientInstance.getEndDate().indexOf('0')>-1?transientInstance.getEndDate():CurrentTime.getCurrentDate2())
						+ "',remark='"
						+ transientInstance.getRemark()
						+ "',Creator_Name='"
						+ transientInstance.getCreatorName()
						+ "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime() + "' where Camp_Id="
						+ transientInstance.getCampId();
			else
				sql = "insert into " + db + ".Camp_Camp values(, '"
						+ transientInstance.getLatnId() + "' , '"
						+ transientInstance.getCampName() + "' , '"
						+ transientInstance.getTargetCustDesc() + "' , '"
						+ transientInstance.getClusterType() + "' , '"
						+ transientInstance.getCampTarget() + "' , '"
						+ transientInstance.getSchemeBook() + "' , '"
						+ transientInstance.getPrdInstDesc() + "' , '"
						+ transientInstance.getCampScript() + "' , '"
						+ transientInstance.getCampStatus() + "',date '" 
						+ (transientInstance.getStartDate()!=null&&transientInstance.getStartDate().indexOf('0')>-1?transientInstance.getStartDate():CurrentTime.getCurrentDate2())
						+ "',date '" 
						+(transientInstance.getEndDate()!=null&&transientInstance.getEndDate().indexOf('0')>-1?transientInstance.getEndDate():CurrentTime.getCurrentDate2())
						+ "', '"
						+ transientInstance.getRemark() + "' , '"
						+ transientInstance.getCreatorName() + "' ,timestamp '" + CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			
			
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampCamp persistentInstance) {

		String sql = "delete from " + db + ".Camp_Camp where Camp_Id = "
				+ persistentInstance.getCampId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampCamp findById(String id) {
        if(null==instlist)
        	findAll();
        
        for(int i=0;i<instlist.size();i++)
        	if(((CampCamp)instlist.get(i)).getCampId()==Integer.parseInt(id))
        	    return (CampCamp)instlist.get(i);
        
		String sql = "select * from " + db + ".Camp_Camp where Camp_Id = " + id;
		CampCamp sd = new CampCamp();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setCampId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setCampName(rs.getString(3));
				sd.setTargetCustDesc(rs.getString(4));
				sd.setClusterType(rs.getString(5));
				sd.setCampTarget(rs.getString(6));
				sd.setSchemeBook(rs.getString(7));
				sd.setPrdInstDesc(rs.getString(8));
				sd.setCampScript(rs.getString(9));
				sd.setCampStatus(rs.getString(10));
				sd.setStartDate(rs.getString(11));
				sd.setEndDate(rs.getString(12));
				sd.setRemark(rs.getString(13));
				sd.setCreatorName(rs.getString(14));
				sd.setCreateTime(rs.getString(15));
				instlist.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll() {

		String sql = "select a.*,b.latn_name from " + db + ".Camp_Camp a join " + db + ".dmn_loc_latn b on a.latn_id = b.latn_id";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampCamp sd = new CampCamp();
				sd.setCampId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setCampName(rs.getString(3));
				sd.setTargetCustDesc(rs.getString(4));
				sd.setClusterType(rs.getString(5));
				sd.setCampTarget(rs.getString(6));
				sd.setSchemeBook(rs.getString(7));
				sd.setPrdInstDesc(rs.getString(8));
				sd.setCampScript(rs.getString(9));
				sd.setCampStatus(rs.getString(10));
				sd.setStartDate(rs.getString(11));
				sd.setEndDate(rs.getString(12));
				sd.setRemark(rs.getString(13));
				sd.setCreatorName(rs.getString(14));
				sd.setCreateTime(rs.getString(15));
				sd.setLatnName(rs.getString(16));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;

	}
	
	public List findAll(String latnId) {

		String sql = "select * from " + db + ".Camp_Camp where Latn_Id='" + latnId + "'"; 
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampCamp sd = new CampCamp();
				sd.setCampId(rs.getInt(1));
				sd.setLatnId(rs.getString(2));
				sd.setCampName(rs.getString(3));
				sd.setTargetCustDesc(rs.getString(4));
				sd.setClusterType(rs.getString(5));
				sd.setCampTarget(rs.getString(6));
				sd.setSchemeBook(rs.getString(7));
				sd.setPrdInstDesc(rs.getString(8));
				sd.setCampScript(rs.getString(9));
				sd.setCampStatus(rs.getString(10));
				sd.setStartDate(rs.getString(11));
				sd.setEndDate(rs.getString(12));
				sd.setRemark(rs.getString(13));
				sd.setCreatorName(rs.getString(14));
				sd.setCreateTime(rs.getString(15));
				list.add(sd);
			}
			
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;

	}

}